package com.hzhr.extdevice.aspect;

import com.hzhr.extdevice.constant.CacheConst;
import com.hzhr.extdevice.constant.ReturnCodeConst;
import com.hzhr.extdevice.model.database.App;
import com.hzhr.extdevice.model.response.BaseResponse;
import com.hzhr.extdevice.util.StringHelper;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by lq on 2018/3/17.
 */
@Aspect
@Component
@Order(2)
public class AuthorAspect {
    /**
     * 日志服务
     */
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Pointcut("execution(* com.hzhr.extdevice.controller..*(..))")
    public void permissionPointCut() {
    }

    @Before("permissionPointCut()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {
    }

    @AfterReturning(returning = "ret", pointcut = "permissionPointCut()")
    public void doAfterReturning(Object ret) throws Throwable {
    }

    @Around("permissionPointCut()&&@annotation(author)")
    public Object doAround(ProceedingJoinPoint joinPoint, Author author) throws Throwable {
    	 // 接收到请求，记录请求内容
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        //region 权限验证
        String securityKey = request.getParameter("token");
        if (StringHelper.isTrimEmpty(securityKey)) {
            return new BaseResponse(ReturnCodeConst.NO_LOGIN, "请登录后重试");
        }

        //endregion
        //执行方法
        return joinPoint.proceed();
    }
}
